package com.easyweb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.easyweb.cache.StartUpListener;
import com.easyweb.common.Constants;
import com.easyweb.common.FileUtils;
import com.easyweb.common.PrimaryKeyGenerator;
import com.easyweb.common.PrintUtils;
import com.easyweb.model.PollingPicture;
import com.easyweb.model.ResultMsg;
import com.easyweb.model.Users;
import com.easyweb.service.PollingPictureService;

@Controller
@RequestMapping("/polling")
public class PollingPictureController {
	
	@Autowired
	private PollingPictureService pollingPictureService;
	
	@RequestMapping(value="/merge",method=RequestMethod.POST)
	public String add(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			request.setAttribute("result", JSONObject.toJSONString(new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE)));
			return "back/frame";
		}
		String id = request.getParameter("id");
		String title = request.getParameter("title");
		if(StringUtils.isEmpty(title)){
			request.setAttribute("result", JSONObject.toJSONString(new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("title.notnull"))));
			return "back/frame";
		}
		String description = request.getParameter("description");
		if(StringUtils.isEmpty(description)){
			request.setAttribute("result", JSONObject.toJSONString(new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("description.notnull"))));
			return "back/frame";
		}
		String href = request.getParameter("href");
		if(StringUtils.isEmpty(href)){
			request.setAttribute("result", JSONObject.toJSONString(new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("url.notnull"))));
			return "back/frame";
		}
		String target = request.getParameter("target");
		if(StringUtils.isEmpty(target)){
			request.setAttribute("result", JSONObject.toJSONString(new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("target.notnull"))));
			return "back/frame";
		}
		String path = this.getClass().getClassLoader().getResource("/").getPath();
		path = path.replace("/WEB-INF/classes", "");
		//上传图片
		List<String> uploadFile = null;
		try {
			uploadFile = FileUtils.uploadFile(request, path+Constants.POLLINGPATH+"/");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("result", JSONObject.toJSONString(new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("image.upload.fail"))));
			return "back/frame";
		}
		if(StringUtils.isEmpty(id)){
			if(uploadFile == null || uploadFile.size() == 0){
				request.setAttribute("result", JSONObject.toJSONString(new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("image.upload.fail"))));
				return "back/frame";
			}
		}
		PollingPicture picture = new PollingPicture();
		if(StringUtils.isEmpty(id)){
			//新增
			picture.setId(PrimaryKeyGenerator.generatePrimaryKey());
			picture.setSortNum(0);
		}else{
			picture.setId(id);
		}
		picture.setTitle(title);
		picture.setDescription(description);
		picture.setHref(href);
		picture.setTarget(target);
		if(uploadFile!=null&&uploadFile.size()>0){
			picture.setImgUrl(Constants.POLLINGPATH+"/"+uploadFile.get(0));
		}
		int num = 0;
		if(StringUtils.isEmpty(id)){
			num = pollingPictureService.insertPollingPicture(picture);
		}else{
			num = pollingPictureService.updatePollingPicture(picture);
		}
		if(num > 0){
			request.setAttribute("result", JSONObject.toJSONString(new ResultMsg()));
		}else{
			request.setAttribute("result", JSONObject.toJSONString(new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("error.unkonw"))));
		}
		return "back/frame";
	}
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public void list(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		List<PollingPicture> pictures = pollingPictureService.getPollingPictures(map);
		PrintUtils.printMsg(response, pictures);
	}
	
	@RequestMapping(value="/get",method=RequestMethod.GET)
	public void get(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		if(StringUtils.isEmpty(id)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("id.notnull")));
			return;
		}
		PollingPicture picture = pollingPictureService.getPollingPictureById(map);
		PrintUtils.printMsg(response, picture);
	}
	
	@RequestMapping(value="/del",method=RequestMethod.GET)
	public void del(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isEmpty(id)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("id.notnull")));
			return;
		}
		map.put("id", id);
		PollingPicture picture = pollingPictureService.getPollingPictureById(map);
		int num = pollingPictureService.deletePollingPictureById(map);
		if(num > 0){
			//删除关联的图片
			String path = this.getClass().getClassLoader().getResource("/").getPath();
			path = path.replace("/WEB-INF/classes", "");
			FileUtils.deleteFile(path+"/"+picture.getImgUrl());
			PrintUtils.printMsg(response, new ResultMsg());
		}else{
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR,null));
		}
	}
	
}
